package logic

import (
	"context"
	"errors"
	"fmt"
	"shuqi-novel/models/cache"
	"time"

	"github.com/mojocn/base64Captcha"
	"github.com/zeromicro/go-zero/core/logx"
	"shuqi-novel/shuqirpc/internal/svc"
	"shuqi-novel/shuqirpc/shuqirpc"
)

type CaptchaLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCaptchaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CaptchaLogic {
	return &CaptchaLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CaptchaLogic) Captcha(in *shuqirpc.CaptchaRequest) (*shuqirpc.CaptchaResponse, error) {
	// 生成图形验证码
	code := base64Captcha.NewDriverDigit(80, 240, 4, 0.7, 80)
	c := base64Captcha.NewCaptcha(code, base64Captcha.DefaultMemStore)
	generate, s, s2, err := c.Generate()
	if err != nil {
		return nil, errors.New(err.Error())
	}
	fmt.Println(generate)

	cache.RDB.Set(l.ctx, "captcha"+in.Account, s2, time.Minute*2)

	return &shuqirpc.CaptchaResponse{
		CaptchaImage: s,
	}, nil
}
